<?php
        
        class PExtensionManager
        {
       		var $puzzle;
       		
       		var $loaded_extensions;
       		var $current_extension;
       		
       		
		function PExtensionManager(&$owner)
		{
			$this->puzzle = $owner;
			$this->loaded_extensions = array();
		}
		
		function useExtension($extension)
		{
			$this->current_extension = $extension;
			
			if (!$this->isInitialized($extension) && $extension != "")
				$this->initializeExtension($extension);
		}
		
		function initializeExtension($extension)
		{
			$DS = $this->puzzle->config['DS'];
			
			include $this->puzzle->config['dir_ext'] . $DS . $extension . $DS . "init.php";
                        $this->loaded_extensions[] = $extension;
		}
		
		function isInitialized($extension)
		{
			if (in_array($extension, $this->loaded_extensions))
				return true;
			else
				return false;
		}
		
		function existExtension($extension)
		{
			$DS = $this->puzzle->config['DS'];
			
			if (file_exists($this->puzzle->config['dir_ext'] . $DS . $extension))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		function installExtension($extension)
		{
			$DS = $this->puzzle->config['DS'];
			//die("alla voy..");
			
			if ($this->existExtension($extension))
			{
				$ifolder = $this->puzzle->config['dir_ext'] . $DS . $extension . $DS . $this->puzzle->config['dir_ext_install'];
				$ifile = $ifolder  . $DS . "install.php";
				
				if (file_exists($ifile))
				{
					include $ifile;
				}
				else
				{
					die("Puzzle :: Error installing extension :: Extension $extension does not have installation files");
				}
			}
			else
			{
				die("Puzzle :: Error installing extension :: Extension $extension does not exist");
			}
		}
        }
        
?>
